<?php

/**
 * ajax返回数据
 * @param int $status 状态码
 * @param string $msg 描述
 * @param array $data 数据
 * @return string
 */
if (!method_exists('ajax_return')) {
    function ajax_return($status = 1, $msg = '', $data = [])
    {
        $res['status'] = $status;
        $res['msg'] = $msg;
        $res['data'] = $data;
        exit(json_encode($res));
    }
}

/**
 * 时间日期转时间戳格式，精确到毫秒，
 */
if (!method_exists('get_data_format')) {
    function get_data_format($time)
    {
        list($usec, $sec) = explode(".", $time);
        $date = strtotime($usec);
        $return_data = str_pad($date . $sec, 13, "0", STR_PAD_RIGHT); //不足13位。右边补0
        return $return_data;
    }
}

/**
 * 去掉数字逗号
 * @param $str
 * @return string
 */
if (!method_exists('get_data_format')) {
    function str_d($str)
    {
        $arr = explode(',', $str);
        $newstr = '';
        foreach ($arr as $k => $v) {
            $newstr .= $v;
        }

        return $newstr;
    }
}

/**
 * 生成唯一订单号
 * @return string
 */
if (!method_exists('buildTradeNo')) {
    function buildTradeNo()
    {
        $tradeNo = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        //检测是否存在
        $info = M('order')->where(['order_no' => $tradeNo])->find();
        (!empty($info)) && $tradeNo = buildTradeNo();
        return $tradeNo;
    }
}

if (!method_exists('statusMsg')) {
    function statusMsg()
    {
        return [
            '未下单',
            '待审核',
            '审核未通过',
            '审核通过',
            '上画待验收',
            '下画待验收',
            '订单完成'
        ];
    }
}

/**
 * 记录日志
 */
if (!method_exists('orderLog')) {
    function orderLog($order_no, $status, $user, $remark, $type = 'add')
    {
        //订单数据
        $order = M('order')->where(['order_no' => $order_no])->find();
        //状态描述
        $name = statusMsg()[$status];
        $data['order_id'] = $order['id'];
        $data['order_no'] = $order_no;
        $data['status'] = $status;
        $data['name'] = $name;
        $data['user_id'] = $user['user_id'];
        $data['user_name'] = $user['user_name'];
        $data['remark'] = $remark;
        $data['add_time'] = time();

        if ($type == 'add') {
            $rs = M('order_status_log')->add($data);
            if (!$rs) {
                return false;
            }
            return true;
        } else {
            $rs = M('order_status_log')->where(['order_no' => $order_no])->save($data);
            if ($rs !== false) {
                return true;
            }
            return false;
        }
    }
}

/**
 * 记录日志 -- 师傅端
 * @param $order_no
 * @param $status
 * @param $user
 * @param $remark
 * @param $type
 */
if (!method_exists('orderWorkerLog')) {
    function orderWorkerLog($order_no, $status, $user, $remark, $type = 'add')
    {
        //订单数据
        $order = M('order')->where(['order_no' => $order_no])->find();
        //状态描述
        //$name = statusMsg()[$status];
        $data['order_id'] = $order['id'];
        $data['order_no'] = $order_no;
        $data['name'] = $remark;
        $data['worker_status'] = $status;
        $data['user_id'] = $user['id'];
        $data['user_name'] = $user['name'];
        $data['remark'] = $remark;
        $data['add_time'] = time();

        if ($type == 'add') {
            $rs = M('order_status_log')->add($data);
            if (!$rs) {
                return false;
            }
            return true;
        } else {
            $rs = M('order_status_log')->where(['order_no' => $order_no])->save($data);
            if ($rs !== false) {
                return true;
            }
            return false;
        }
    }
}


/**
 * 短信记录
 * @param $order_no
 * @param $user_name
 * @param $user_mobile
 * @param $result
 * @param $type 1通知 2 验证码
 * @param $code
 * @param $remark
 */
if (!method_exists('smsLog')) {
    function smsLog($order_no, $user_name, $user_mobile, $result, $type, $code, $remark)
    {
        //订单数据
        $data['order_no'] = $order_no;
        $data['user_name'] = $user_name;
        $data['user_mobile'] = $user_mobile;
        $data['result'] = $result;
        $data['type'] = $type;
        $data['code'] = $code;
        $data['remark'] = $remark;
        $data['add_time'] = time();
        $rs = M('sms_log')->add($data);
    }
}


if(!method_exists('format_date')){
    function format_date($time){
        return date('Y-m-d H:i:s', $time);
    }
}

if(!method_exists('makeImagePath')){
    function makeImagePath($path){
        return 'http://'.$_SERVER['HTTP_HOST'].'/'.$path;
    }
}
